<extend name="Public:innerbase" />
<block name="css">
<link rel="stylesheet" href="{:STATICS}/validation/css/validationEngine.jquery.css">
</block>
<block name="js">
<include file="Public:upload" />
<include file="Public:editor" />
<script type="text/javascript" src="{:STATICS}/validation/js/jquery.validationEngine.js"></script>
<script type="text/javascript" src="{:STATICS}/validation/js/jquery.validationEngine-zh_CN.js"></script>
    <script>
        function changeNeedSms(){
         $.ajax({
             url:"{:U('Dining/sms_need')}",
             type:"POST",
             data:{id:$("#id").val(), need_sms:$("#need_sms").val()},
             dataType:"text",
             success:function(data){
                if(data == 1){
                    $("#txt").html("修改成功！");
                }else{
                    $("#txt").html("修改失败!");
                }
             }
         });
         }

        function receiverChange(){
            var txt = $("#receiver").val();
            var str = "";
            for(var i = 0; i < txt.length; i++){
                var c = txt.charAt(i);
                if(c != ',' && c < '0' || c > '9'){
                    if(c != " "){
                        str += ",";
                    }
                }else{
                    str +=c;
                }
            }
            $("#receiver").val(str);
        }

        function trim(str){ //删除左右两端的空格
             return str.replace(/(^\s*)|(\s*$)/g, "");
        }

        function save(){
            var txt = $("#receiver").val();
            var l = $("#receiver").attr("ori");
            var arr = txt.split(",");
            txt="";
            for(var i = 0; i < arr.length; i++){
                var t = trim(arr[i]);
                if( t && t != ""){
                    var re= /^0?(13[0-9]|15[012356789]|18[0236789]|14[57])[0-9]{8}$/
                    if(!re.test(t)){
                        alert("手机号码填写有误，请检查。");
                        return;
                    }else{
                        if(txt != ""){
                            txt += ",";
                        }
                        txt += t;
                    }
                }
            }
            if(l == txt){
                return;
            }
            $("#save-btn").attr("disabled","true");
            $.ajax({
                url:"{:U('Dining/sms_save')}",
                type:"POST",
                async: false,
                data:{id:$("#id").val(), receiver:txt},
                dataType:"text",
                success:function(data){
                    if(data == 1){
                        $("#r_msg").html("保存成功！");
                    }else{
                        $("#r_msg").html("保存失败!");
                    }
                }
            });
            $("#save-btn").removeAttr("disabled");
        }


    </script>
</block>
<block name="content">
    <style>
        #txt,#r_msg{
            float: left;
        }
    </style>
<div class="container-fluid">
	<div class="page-header wm-page-header">
		<h4>短信设置</h4>
		<a href="{:U('Dining/index')}" class="btn btn-default btn-sm">
			<span class="glyphicon glyphicon-chevron-left"></span>&nbsp;返回
		</a>
	</div>
	<form id="editForm" class="form-horizontal" >
		<input id="id" type="hidden" name="id" value="{$sms.id}" />
		<div class="form-group">
			<label class="col-sm-2">是否启用短信提醒</label>
			<div class="col-md-2">
				<select id="need_sms" name="need_sms" class="form-control" onchange="changeNeedSms()">
					<option value="0">停用</option>
                    <option value="1">启用</option>
				</select>
                <script>$("#need_sms").val("{$sms.need_sms}")</script>
			</div><span id="txt"></span>
		</div>
		<div class="form-group">
			<label class="col-sm-2">短信剩余条数</label>
			<div class="col-md-2">
				<div class="input-group">
                    {$sms.sms_count}
					<span >条</span>
				</div>
			</div>
		</div>
        <div class="form-group">
            <label class="col-sm-2">短信接收人</label>
            <div class="col-md-4">
                <input id="receiver" onchange="receiverChange()" type="text" name="intro" ori="{$sms.receiver}"
                       value="{$sms.receiver}" class="form-control" /><span id="r_msg"></span>
            </div>
        </div>
        <input id="save-btn" type="button" class="btn btn-primary" value="保存"  onclick="save()" >
	</form>
</div>
</block>